package cn.wolfcode.p2p.web.interceptor;

import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.util.UserContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CheckLoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 动态资源才去判断,静态资源直接放行
        if (handler instanceof HandlerMethod) {
            LoginInfo currentUser = UserContext.getCurrentUser();
            if (currentUser == null) {
                response.sendRedirect("/login.html");
                return false;
            }
        }

        return true;
    }
}
